﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using USBXpressNET;

namespace USBXpressNETTest
{
    public class TestHelper
    {
        private static int _numDevices;
        private static uint[] _handles;
        public static uint[] Handles
        {
            get { return _handles; }
        }
        public static int NumDevices
        {
            get
            {
                if (_numDevices == 0) _numDevices = GetNumDevices();
                return _numDevices;
            }
        }

        static TestHelper()
        {
            _handles = NumDevices == 0 ? new uint[1] : new uint[NumDevices];
            for(int i = 0; i < NumDevices; i++)
            {
                _handles[i] = GetHandle(i);
            }
        }

        private static int GetNumDevices()
        {
            int devices = 0;
            var retVal = API.GetNumDevices(out devices);
            Assert.AreEqual(ReturnValue.SI_SUCCESS, retVal);
            Assert.AreNotEqual(0, devices);
            return devices;
        }

        private static uint GetHandle(int index)
        {
            uint handle = 0;
            ReturnValue retVal = API.Open(index, out handle);
            Assert.AreEqual(ReturnValue.SI_SUCCESS, retVal);
            Assert.AreNotEqual(0, handle);
            return handle;
        }


    }
}
